package com.gridpower.spreadsheet.client.widgets;

//import com.google.gwt.dom.client.Element;
import java.util.ArrayList;
import java.util.Iterator;

import com.google.gwt.user.client.DOM;
import com.google.gwt.user.client.Element;
import com.google.gwt.user.client.ui.AbsolutePanel;
import com.google.gwt.widgetideas.graphics.client.Color;

public class TextLayer extends AbsolutePanel {
	/**
	 * a transparent absolutePanel
	 *
	 */
	ArrayList txtElems = new ArrayList();
	public TextLayer() {
		super();
		DOM.setStyleAttribute(getElement(), "backgroundColor", "transparent");
	}
	/**
	 * create a new div and draw some text
	 *
	 */
	public void drawText() {
		
	}
	public void clearElems() {
		Iterator itr = txtElems.iterator();
		while (itr.hasNext()) {
			Element e = (Element)itr.next();
			DOM.setStyleAttribute(e, "display", "none");
			//DOM.removeChild(this.getElement(), e);
			//UIUtil.getElementPool().returnElement(e);
		}
	}
	/**
	 * draw on an existed div, if the div is already the child of this panel
	 * just set its inner text, if the div is not the child, add it as a child
	 * x, y is the relative postion in the absolutepanel 
	 * todo: consider the alignment and the auto-suitable font-size
	 */
	public void drawTextInElement(Element location, double x, double y, double width, double height,
			String content, String fontFamily, String fontWeight, String fontSize, String color) {
		if (location != null) {
			if (DOM.isOrHasChild(this.getElement(), location) == false) {
				//DOM.removeChild(, location);??
				//DOM.removeChild(DOM.getParent(location), location);
				txtElems.add(location);
				DOM.appendChild(this.getElement(), location);
			}
			if (fontFamily == null) 
				fontFamily = "Times New Roman";
			if (fontWeight == null)
				fontWeight = "normal";
			if (fontSize.equals("0") ==true)
				fontSize = "9";
			if (color == null)
				color = "red";
		    //DOM.setStyleAttribute(location, "left", "");
		    //DOM.setStyleAttribute(location, "top", "");
			DOM.setStyleAttribute(location, "display", "block");
		    DOM.setStyleAttribute(location, "position", "absolute");
		    DOM.setStyleAttribute(location, "left", x + "px");
		    DOM.setStyleAttribute(location, "top", y + "px");
		    DOM.setStyleAttribute(location, "width", width + "px");
		    DOM.setStyleAttribute(location, "height", height + "px");
		    DOM.setStyleAttribute(location, "overflow", "hidden");
		    
		    DOM.setStyleAttribute(location, "fontFamily", fontFamily);
		    DOM.setStyleAttribute(location, "fontSize", fontSize);
		    //DOM.setStyleAttribute(location, "fontWeight", fontWeight);
		    DOM.setStyleAttribute(location, "color", color);
		    // DOM.setStyleAttribute(location, "opacity", getTransparency());
		    DOM.setInnerHTML(location, content);
		}
		
	}
}
